package com.vogue.family.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.SimpleItemAnimator;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.vogue.component.adapter.ViewHolderDelegate;
import com.vogue.component.adapter.VogueRecyclerAdapter;
import com.vogue.component.permission.RxPermissions;
import com.vogue.family.R;
import com.vogue.family.data.entity.Meeting;
import com.vogue.family.data.entity.NoMoreData;
import com.vogue.family.databinding.ActivityMainBinding;
import com.vogue.family.databinding.ItemMeetingBinding;
import com.vogue.family.databinding.NoMoreDataBinding;
import com.vogue.family.ui.callback.RecyclerViewItemClickListener;
import com.vogue.family.ui.fragment.CreateMeetingFragment;
import com.vogue.family.ui.vm.MainViewModel;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.jitsi.meet.sdk.JitsiMeetActivity;
import org.jitsi.meet.sdk.JitsiMeetConferenceOptions;
import org.jitsi.meet.sdk.JitsiMeetUserInfo;

public class MainActivity extends AppCompatActivity  implements RecyclerViewItemClickListener<Meeting>, OnRefreshLoadMoreListener {

    private ActivityMainBinding binding;
    private MainViewModel viewModel;

    private VogueRecyclerAdapter adapter;
    protected boolean refresh=true;

    /**
     *请求起始点 binding.srlList.autoRefresh(); 这个执行一次刷新请求{@link MainActivity#onRefresh }
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        viewModel = new ViewModelProvider(this).get(MainViewModel.class);


        RxPermissions rxPermissions=new RxPermissions(this);
        rxPermissions.requestEach(Manifest.permission.READ_EXTERNAL_STORAGE).doOnNext(permission -> {
            if (permission.granted){

            }
        });


        //创建会议
        binding.createMeeting.setOnClickListener(view -> {
            CreateMeetingFragment createMeetingFragment = CreateMeetingFragment.newInstance();
            createMeetingFragment.show(getSupportFragmentManager(),"createMeetingFragment");
        });


        adapter = new VogueRecyclerAdapter(new ArrayList());

        adapter.addViewHolder(R.layout.item_meeting, new ViewHolderDelegate<Meeting, ItemMeetingBinding>() {
            @Override
            public void onBindingView(ItemMeetingBinding binding, Meeting data, int position) {
                binding.setData(data);
                binding.setOnRecyclerViewItemClickListener(MainActivity.this);
            }
        });

        adapter.addViewHolder(R.layout.no_more_data, new ViewHolderDelegate<NoMoreData, NoMoreDataBinding>() {
            @Override
            public void onBindingView(NoMoreDataBinding binding, NoMoreData data, int position) {
                binding.tvNoMoreMsg.setText(data.getMsg());
            }
        });

        StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
        binding.rvList.setLayoutManager(layoutManager);

        ((SimpleItemAnimator)binding.rvList.getItemAnimator()).setSupportsChangeAnimations(false);
        binding.rvList.getItemAnimator().setChangeDuration(0);
        binding.rvList.setAdapter(adapter);

        binding.srlList.autoRefresh();

        binding.srlList.setNestedScrollingEnabled(true);
        binding.srlList.setOnRefreshListener(this);
        binding.srlList.setOnLoadMoreListener(this);
        binding.srlList.setNoMoreData(false);



        viewModel.getErrorMsg().observe(this ,s -> {
            Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
        });

        viewModel.getList().observe(this,meetings -> {
            if (meetings.isEmpty()) {
                Toast.makeText(MainActivity.this, "没有获取到数据", Toast.LENGTH_LONG).show();
                binding.srlList.finishRefresh();
                binding.srlList.finishLoadMore();
            } else {
                if (meetings.size()<10){
                    binding.srlList.setNoMoreData(true);
                    binding.srlList.finishLoadMoreWithNoMoreData();
                    binding.srlList.finishRefreshWithNoMoreData();
                }else {
                    binding.srlList.finishRefresh();
                    binding.srlList.finishLoadMore();
                }
                if (refresh) {
                    adapter.refreshData(meetings);
                } else {
                    adapter.loadMoreData(meetings);
                }
            }
        });

        JitsiMeetConferenceOptions options =new JitsiMeetConferenceOptions.Builder()
                .setUserInfo(new JitsiMeetUserInfo())
                .setToken("")
                .build();
/*        binding.btnJoin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (binding.edRoom.getText().toString().isEmpty()){
                    Toast.makeText(MainActivity.this,"请输入房间ID", Toast.LENGTH_LONG).show();
                } else{
                    try {
                        JitsiMeetConferenceOptions options = new JitsiMeetConferenceOptions.Builder()
                                .setServerURL(new URL("https://meet.jit.si"))
                                .setRoom(binding.edRoom.getText().toString())
                                .setAudioOnly(true)
                                .build();

                        JitsiMeetActivity.launch(MainActivity.this,options);

                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                }
            }
        });*/

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        viewModel.getMeeting(refresh=false);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        viewModel.getMeeting(refresh=true);
    }

    @Override
    public void onRecyclerViewItemClick(Meeting data) {

        JitsiMeetConferenceOptions options =new JitsiMeetConferenceOptions.Builder()
                .setUserInfo(new JitsiMeetUserInfo())
                .setAudioOnly(true)
                .setToken("")
                .build();

        JitsiMeetActivity.launch(MainActivity.this,options);

    }
}